<template>
  <div>
    <dialog-content :dialogOptions="dialogOptions || (new Date().getTime())">
      <template slot="dialog-content_content">
        <form-content ref="scheduledInfoForm" :formOptions="formOptions" :formSource="formSource" @changeHandle="changeHandle">
        </form-content>
      </template>
      <template slot="dialog-content_footer">
        <el-button v-if="add && isAuth('scheduled:task:save')" type="primary" v-prevent-re-click
         :size="this.size" @click="submitHandle('save')">保 存</el-button>
        <el-button v-if="change && isAuth('scheduled:task:update') && stateRunShow && protect" type="primary" v-prevent-re-click
         :size="this.size" @click="submitHandle('edit')">修 改</el-button>
        <el-button v-if="change && isAuth('scheduled:task:delete') && stateRunShow && protect" type="danger" v-prevent-re-click
         :size="this.size" @click="submitHandle('delete')">删 除</el-button>
        <el-button :size="this.size" @click="dialogOptions.show = false">取 消</el-button>
      </template>
    </dialog-content>
  </div>
</template>

<script>
import { getScheduledInfoApi, saveScheduledApi, updateScheduledApi, deleteScheduledApi } from '@/api/scheduled'
import DialogContent from '@/components/common/dialog-content'
import FormContent from '@/components/common/form-content'
export default {
  components: { DialogContent, FormContent },
  props: {
    size: {
      type: String,
      default: 'small'
    }
  },
  data() {
    return {
      dialogOptions: {
        title: '调度任务信息',
        show: false,
        modal: true,
        width: '580px',
        height: '360px',
        center: true
      },
      formOptions: {
        inline: true,
        labelPosition: 'right',
        labelWidth: '80px',
        size: this.size
      },
      formSource: {
        operations: [
          {
            name: '名称',
            key: 'name',
            type: 'text',
            width: '185px',
            length: 15
          },
          {
            name: '表达式',
            key: 'cron',
            type: 'text',
            width: '185px',
            length: 50
          },
          {
            name: 'Bean',
            key: 'beanName',
            type: 'text',
            width: '460px',
            length: 128
          },
          {
            name: '创建人',
            key: 'createName',
            type: 'text',
            width: '185px',
            disabled: true
          },
          {
            name: '创建时间',
            key: 'createTime',
            type: 'datetime',
            width: '185px',
            disabled: true
          },
          {
            name: '修改人',
            key: 'updateName',
            type: 'text',
            width: '185px',
            disabled: true
          },
          {
            name: '修改时间',
            key: 'updateTime',
            type: 'datetime',
            width: '185px',
            disabled: true
          },
          {
            name: '初始执行',
            key: 'initStartFlag',
            type: 'switch',
            width: '35px'
          },
          {
            name: '失败通知',
            key: 'failNotice',
            type: 'switch',
            width: '35px'
          },
          {
            name: '守护任务',
            key: 'protect',
            type: 'switch',
            width: '35px'
          },
          {
            name: '状态',
            key: 'status',
            type: 'select',
            width: '85px',
            disabled: true,
            list: [
              { key: '未启动', value: 0 },
              { key: '运行', value: 1 },
              { key: '停止', value: 2 }
            ]
          },
          {
            name: '执行结果',
            key: 'executionStatus',
            type: 'select',
            disabled: true,
            list: [
              { key: '未执行', value: 0 },
              { key: '运行', value: 1 },
              { key: '停止', value: 2 },
              { key: '成功', value: 3 },
              { key: '失败', value: 4 }
            ]
          },
          { type: 'place', width: '55px' },
          {
            name: '执行时间',
            key: 'executionLastTime',
            type: 'datetime',
            width: '185px',
            disabled: true
          },
          {
            name: '备注',
            key: 'remark',
            type: 'textarea',
            width: '460px',
            length: 500
          }
        ],
        ruleForm: {},
        rules: {
          name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
          beanName: [{ required: true, message: 'Bean不能为空', trigger: 'blur' }],
          cron: [{ required: true, message: '表达式不能为空', trigger: 'blur' }]
        }
      },
      type: 0, // 0添加1查看
      stateRunShow: true,
      protect: false // true守护
    }
  },
  computed: {
    add: {
      get() { return this.type === 0 }
    },
    change: {
      get() { return this.type === 1 }
    }
  },
  methods: {
    init(type = 0, id = 0) {
      this.type = type
      if (id !== 0 && type === 1) {
        getScheduledInfoApi(id).then(res => {
          let data = res.data
          this.formSource.ruleForm = {
            id: data.id,
            createName: data.createName,
            createTime: data.createTime ? this.$time.formatDate(data.createTime, 'yyyy-MM-dd HH:mm:ss') : '',
            updateName: data.updateName,
            updateTime: data.updateTime ? this.$time.formatDate(data.updateTime, 'yyyy-MM-dd HH:mm:ss') : '',
            name: data.name,
            beanName: data.beanName,
            cron: data.cron,
            initStartFlag: data.initStartFlag,
            failNotice: data.failNotice,
            protect: data.protect,
            status: data.status,
            executionStatus: data.executionStatus,
            executionLastTime: data.executionLastTime ? this.$time.formatDate(data.executionLastTime, 'yyyy-MM-dd HH:mm:ss') : '',
            remark: data.remark
          }

          // 如果是运行状态隐藏修改和删除按钮
          this.stateRunShow = data.status !== 1
          this.protect = this.formSource.ruleForm.protect ? (this.isAuth('scheduled:task:protect')) : true
        }).then(res => {
          this.controlInit(type)
          this.flushForm(this)
        })
      } else {
        this.formSource.ruleForm = {
          id: '0',
          name: '',
          beanName: '',
          cron: '',
          initStartFlag: false,
          failNotice: false,
          protect: false,
          status: 0,
          remark: ''
        }
        this.controlInit(type)
        this.flushForm(this)
      }
    },
    flushForm(target) {
      if (target.$refs.scheduledInfoForm === undefined) {
        target.$nextTick(() => {
          target.$refs.scheduledInfoForm.init()
        })
      } else {
        if (this.type === 0) {
          target.$refs.scheduledInfoForm.init()
        } else {
          target.$refs.scheduledInfoForm.update()
          target.$refs.scheduledInfoForm.clearValidate()
        }
      }
    },
    changeHandle(obj) {},
    initHeight() {
      if (this.type === 0) {
        return 210
      } else if (this.type === 1) {
        return 360
      }
    },
    dynamicHeight() {
      let height = this.dialogOptions.height
      height = height.substr(0, height.length - 2)

      let initHeight = this.initHeight()
      height = initHeight
      this.dialogOptions.height = height + 'px'
    },
    controlInit(type) {
      this.dynamicHeight()

      this.formSource.operations.forEach(item => {
        if (item.key === 'createName' || item.key === 'createTime' || item.key === 'updateName' || item.key === 'updateTime' ||
          item.key === 'status' || item.key === 'executionStatus' || item.key === 'executionLastTime') {
          if (type === 0) {
            item.show = false
          } else {
            item.show = true
          }
        }
      })
    },
    submitHandle(type = '') {
      if (type === '') return false
      if (!this.$refs.scheduledInfoForm.submitForm()) return false

      if (type === 'save') {
        saveScheduledApi(this.formSource.ruleForm).then(res => {
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1500
          })
          this.dialogOptions.show = false
          this.$emit('flush')
        }).catch(() => {
          this.$message({
            message: '添加失败',
            type: 'error',
            duration: 1500
          })
        })
      } else if (type === 'edit' && this.formSource.ruleForm.id !== '0') {
        updateScheduledApi(this.formSource.ruleForm).then(res => {
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1500
          })
          this.dialogOptions.show = false
          this.$emit('flush')
        }).catch(() => {
          this.$message({
            message: '修改失败',
            type: 'error',
            duration: 1500
          })
        })
      } else if (type === 'delete' && this.formSource.ruleForm.id !== '0') {
        deleteScheduledApi(this.formSource.ruleForm.id).then(res => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1500
          })
          this.dialogOptions.show = false
          this.$emit('flush')
        }).catch(() => {
          this.$message({
            message: '删除失败',
            type: 'error',
            duration: 1500
          })
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
